﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace OISTools
{
    public partial class CheckUpdatePackage : Form
    {
        public CheckUpdatePackage()
        {
            InitializeComponent();
        }

        private void txtPath_TextChanged(object sender, EventArgs e)
        {

        }

        private void btnCheck_Click(object sender, EventArgs e)
        {
            #region //所有文件 存為 UTF-8,如果沒有自動修復。

            string path = txtPath.Text;

            System.IO.DirectoryInfo dInfo = new System.IO.DirectoryInfo(path);
            FileInfo[] fList = dInfo.GetFiles("*.*", System.IO.SearchOption.AllDirectories);
            foreach (FileInfo f1 in fList)
            {

                StreamReader sr = new StreamReader(f1.FullName);

                if (sr.CurrentEncoding != Encoding.UTF8)
                {
                    lboxDetail.Items.Add(string.Format("{0} 編碼不是 UTF-8", f1.FullName));
                }
            }
            #endregion
            #region //檢查根目錄下面是否有 Readme.txt
            if (!File.Exists(Path.Combine(path, "readme.txt")))
            {
                lboxDetail.Items.Add("沒有Readme.txt");
            }

            if (lboxDetail.Items.Count == 0)
            {
                lboxDetail.Items.Add("確認！");
            }
            #endregion
            #region 檢查目錄結構

            //個級目錄下只能有 justgogopg,jgoerp
            var dirList= dInfo.GetDirectories("*", SearchOption.TopDirectoryOnly);
            if (dirList.Length <= 0)
            {
                lboxDetail.Items.Add("沒有有效子目錄");
            }

            foreach (var d1 in dirList)
            {
                if (d1.Name != "justgogopg" && d1.Name != "jgoerp")
                {
                    lboxDetail.Items.Add("一級目錄結構無效");
                }
                else
                {
                    foreach (var d2 in d1.GetDirectories())
                    {
                        if (d2.Name != "SQL" && d2.Name != "XML")
                        {
                            lboxDetail.Items.Add("二級目錄結構無效");
                            if (d2.Name == "SQL")
                            {
                                foreach (var f2 in d2.GetFiles("*.*", SearchOption.AllDirectories))
                                {
                                    if (f2.Extension != "sql")
                                    {
                                        lboxDetail.Items.Add("SQL目錄包含無效文件");
                                    }
                                }
                            }
                            if (d2.Name == "XML")
                            {
                                foreach (var f2 in d2.GetFiles("*.*", SearchOption.AllDirectories))
                                {
                                    if (f2.Extension != "xml")
                                    {
                                        lboxDetail.Items.Add("XML目錄包含無效文件");
                                    }
                                }
                            }
                        }
                    }
                }
            }
            #endregion







        }



        private void button1_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.ShowDialog();
            txtPath.Text = folderBrowserDialog1.SelectedPath;
        }

        private void CheckUpdatePackage_Load(object sender, EventArgs e)
        {
            folderBrowserDialog1.SelectedPath = @"F:\工作相关\ERP\更新歷史";
        }







        //System.Text.Encoding aa = GetType(f1.FullName);
        //if (aa.EncodingName != "Unicode (UTF-8)")

        ///// <summary>
        ///// 给定文件的路径，读取文件的二进制数据，判断文件的编码类型
        ///// </summary>
        ///// <param name="FILE_NAME">文件路径</param>
        ///// <returns>文件的编码类型</returns>
        //public static System.Text.Encoding GetType(string FILE_NAME)
        //{
        //    FileStream fs = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read);
        //    Encoding r = GetType(fs);
        //    fs.Close();
        //    return r;
        //}
        ///// <summary>
        ///// 通过给定的文件流，判断文件的编码类型
        ///// </summary>
        ///// <param name="fs">文件流</param>
        ///// <returns>文件的编码类型</returns>
        //public static System.Text.Encoding GetType(FileStream fs)
        //{
        //    byte[] Unicode = new byte[] { 0xFF, 0xFE, 0x41 };
        //    byte[] UnicodeBIG = new byte[] { 0xFE, 0xFF, 0x00 };
        //    byte[] UTF8 = new byte[] { 0xEF, 0xBB, 0xBF }; //带BOM
        //    Encoding reVal = Encoding.Default;

        //    BinaryReader r = new BinaryReader(fs, System.Text.Encoding.Default);
        //    int i;
        //    int.TryParse(fs.Length.ToString(), out i);
        //    byte[] ss = r.ReadBytes(i);
        //    if (IsUTF8Bytes(ss) || (ss[0] == 0xEF && ss[1] == 0xBB && ss[2] == 0xBF))
        //    {
        //        reVal = Encoding.UTF8;
        //    }
        //    else if (ss[0] == 0xFE && ss[1] == 0xFF && ss[2] == 0x00)
        //    {
        //        reVal = Encoding.BigEndianUnicode;
        //    }
        //    else if (ss[0] == 0xFF && ss[1] == 0xFE && ss[2] == 0x41)
        //    {
        //        reVal = Encoding.Unicode;
        //    }
        //    r.Close();
        //    return reVal;

        //}

        ///// <summary>
        ///// 判断是否是不带 BOM 的 UTF8 格式
        ///// </summary>
        ///// <param name="data"></param>
        ///// <returns></returns>
        //private static bool IsUTF8Bytes(byte[] data)
        //{
        //    int charByteCounter = 1;　 //计算当前正分析的字符应还有的字节数
        //    byte curByte; //当前分析的字节.
        //    for (int i = 0; i < data.Length; i++)
        //    {
        //        curByte = data[i];
        //        if (charByteCounter == 1)
        //        {
        //            if (curByte >= 0x80)
        //            {
        //                //判断当前
        //                while (((curByte <<= 1) & 0x80) != 0)
        //                {
        //                    charByteCounter++;
        //                }
        //                //标记位首位若为非0 则至少以2个1开始 如:110XXXXX...........1111110X　
        //                if (charByteCounter == 1 || charByteCounter > 6)
        //                {
        //                    return false;
        //                }
        //            }
        //        }
        //        else
        //        {
        //            //若是UTF-8 此时第一位必须为1
        //            if ((curByte & 0xC0) != 0x80)
        //            {
        //                return false;
        //            }
        //            charByteCounter--;
        //        }
        //    }
        //    if (charByteCounter > 1)
        //    {
        //        throw new Exception("非预期的byte格式");
        //    }
        //    return true;
        //}
    }


}
